<?php

namespace Melon;

use Symfony\Component\Finder\Finder;

class Application
{
    /**
     * @var string
     */
    protected string $basePath;

    /**
     * @var int
     */
    protected int $count = 0;

    /**
     * @var array
     */
    protected array $workers = [];

    /**
     * @var array
     */
    protected array $instance = [];

    /**
     * @var array
     */
    protected array $config = [];

    /**
     * @param string $basePath
     */
    public function __construct(string $basePath)
    {
        $this->basePath = $basePath;

        $this->count = 1;

        $this->bootstrap();
    }

    protected function bootstrap()
    {
        $this->loadingConfig();
    }

    protected function loadingConfig()
    {
        $finder = new Finder();
        $finder->files()->in($this->basePath() . "/config")->name("*.php");

        foreach ($finder as $file) {
            $this->config[$file->getFilenameWithoutExtension()] = require $file->getRealPath();
        }
    }

    public function openWorker()
    {
        // init worker
        for ($i = 0; $i < $this->count; $i++) {
            $this->workers[$i] = new Worker($this);
            $this->workers[$i]->run();
        }
    }

    public function run() {
        $this->openWorker();
    }

    /**
     * @return string
     */
    public function basePath(): string
    {
        return $this->basePath;
    }

    /**
     * @return string
     */
    public function getAddress(): string
    {
        return $this->config['app']['address'];
    }
}
